/**
 * 点击事件管理器
 */

var UnitTools = require('UnitTools')
var CreatorHelper = require('CreatorHelper')
var UserManager = require('UserManager')
var TableManager = require('TableManager')
var NetworkManager = require('NetworkManager')

class ClickManager {

    /**
     * 构造函数
     *
     * @return {[type]} [description]
     */
    constructor () {
        this.timer = null
    }

    /**
     * 延时点击
     * @param  {[type]}   element  元素
     * @param  {[type]}   _this    类
     * @param  {Function} callback 回调
     * @param  {[type]}   time     延时单位(ms)
     *
     * @return {[type]}            [description]
     */
    static onClick (element, _this, callback, time) {
        var self = this
        time = !time ? 120 : time

        element.on(cc.Node.EventType.TOUCH_END, function (arg) {
            clearTimeout(self.timer)
            self.timer = setTimeout(function () {
                callback(arg)
            }, time)
        }, _this)
    }

    /**
     * 提示框
     *
     * @param  {[type]} prefab [description]
     * @param  {[type]} msg    [description]
     * @param  {[type]} scene  [description]
     * @return {[type]}        [description]
     */
    static alertModel (prefab, msg, scene) {
        let alert = cc.instantiate(prefab)

        if (UnitTools.isNullOrUndefined(scene)) {
            scene = cc.director.getScene()
        }

        alert.parent = scene
        alert.getComponent('Alert').run(msg)
    }

    /**
     * 设置缓存
     *
     * @param  {[type]} $key   [description]
     * @param  {[type]} $value [description]
     * @return {[type]}        [description]
     */
    static ccSetCache (key, value) {
        cc.sys.localStorage.setItem(key, value)
    }

    /**
     * 获取缓存
     *
     * @param  {[type]} key [description]
     * @return {[type]}     [description]
     */
    static ccGetCache (key) {
        let value = cc.sys.localStorage.getItem(key)
        return UnitTools.isNullOrUndefined(value) ? false : value
    }

    /**
     * 加载框
     *
     * @param  {[type]} pnode [description]
     * @return {[type]}       [description]
     */
    static loading (pnode, text) {
        if (pnode.children === null) {
            return false
        }

        var loadingNode = pnode.getChildByName('modelLoading')
        if (loadingNode == null && !ClickManager.loadingStatus) {
            var url = 'prefabs/model/modelLoading'
            ClickManager.loadingStatus = true
            CreatorHelper.loadPrefab(url, (node) => {
                var txt = node.children[1].getComponent(cc.Label)
                var aurl = ['animations/loading']

                CreatorHelper.loadAnimClip(aurl, (clip) => {
                    var animation = node.children[0].addComponent(cc.Animation)
                    var loadingAnim = clip[0]
                    animation.addClip(loadingAnim, 'loading')

                    let state = animation.play('loading')
                    if (state !== null) {
                        state.wrapMode = cc.WrapMode.Loop
                        txt.string = text
                    }

                    pnode.addChild(node)
                })
            })
        } else if (loadingNode != null) {
            var txt = loadingNode.children[1].getComponent(cc.Label)
            txt.string = text
        }
    }

    /**
     * 移除节点
     *
     * @param  {[type]} pnode [description]
     * @return {[type]}       [description]
     */
    static rmLoading (pnode) {
        if (pnode.children === null) {
            return false
        }

        var loadingNode = pnode.getChildByName('modelLoading')
        if (loadingNode != null) {
            pnode.removeChild(loadingNode)
            ClickManager.loadingStatus = false
        } else {
            setTimeout(() => {
                if (pnode.children === null) {
                    return false
                }

                var loadingNode = pnode.getChildByName('modelLoading')
                if (loadingNode != null) {
                    pnode.removeChild(loadingNode)
                    ClickManager.loadingStatus = false
                }
            }, 500)
        }
    }

    /**
     * 退出登录
     *
     * @return {[type]} [description]
     */
    static exitLogin () {
        // 清空数据
        UserManager.token = null    // 用户令牌
        UserManager.loginToGameData = null  // 登录到游戏数据
        UserManager.joinToTableData = null  // 用户加入牌桌数据
        UserManager.recePushToGame  = null  // 接收服务端推送的信息
        UserManager.identity = 0 // 玩家的身份 0-未准备 1-已准备 2-开始游戏
        UserManager.myuid = 0    // 玩家自己的uid
        UserManager.tid   = 0    // 牌桌id
        UserManager.pos   = 0    // 自己的位置
        UserManager.hands = null    // 玩家手牌信息
        UserManager.flows = null    // 游戏玩法流程

        TableManager.user = null
        TableManager.gameing = false
        TableManager.settlement = false
        cc.sys.localStorage.removeItem('__TOKEN__')
        cc.sys.localStorage.removeItem('__USERSTRING__')
        cc.sys.localStorage.removeItem('__PLAYER_BASE_INFO__')
        NetworkManager.clearHallService()
        NetworkManager.clearGameService()
        cc.director.loadScene('login')
    }
}

/**
 * webview充值管理
 *
 * @type {Boolean}
 */
ClickManager.webViewCallBackFlag = false

ClickManager.loadingStatus = false

module.exports = ClickManager
